package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.DormitoryAdmin;
import org.example.form.RuleForm;
import org.example.form.SearchForm;
import org.example.mapper.DormitoryAdminMapper;
import org.example.service.DormitoryAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.util.JwtUtils;
import org.example.vo.PageVO;
import org.example.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@Slf4j
@Service
public class DormitoryAdminServiceImpl extends ServiceImpl<DormitoryAdminMapper, DormitoryAdmin> implements DormitoryAdminService {
   @Autowired
   private DormitoryAdminMapper dormitoryAdminMapper;
    @Override
    public ResultVO login(RuleForm ruleForm) {
        //判断用户名是否存在
        QueryWrapper<DormitoryAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        DormitoryAdmin dormitoryAdmin = this.dormitoryAdminMapper.selectOne(queryWrapper);
         ResultVO resultVO = new ResultVO();
        if (dormitoryAdmin==null){
            resultVO.setCode(-1);
        }else {
            //判断密码是否正确
            if (!dormitoryAdmin.getPassword().equals(ruleForm.getPassword())) {
                resultVO.setCode(-2);
            }else {
                //登陆成功，生成token令牌，下发令牌
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", dormitoryAdmin.getId());
                claims.put("name", dormitoryAdmin.getName());
                claims.put("username", dormitoryAdmin.getUsername());
                String token = JwtUtils.generateJwt(claims);

                resultVO.setCode(0);
                resultVO.setMsg(token);
                resultVO.setData(dormitoryAdmin);
            }
        }
        return resultVO;
    }

    @Override
    public PageVO list(Integer page, Integer size) {
        Page<DormitoryAdmin> dormitoryAdminPage = new Page<>(page, size);
        Page<DormitoryAdmin> dormitoryAdminPage1 = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, null);

        return new PageVO(dormitoryAdminPage1.getRecords(),dormitoryAdminPage1.getTotal());

    }

    @Override
    public PageVO search(SearchForm searchForm ) {
        //模糊查询+分页
        Page<DormitoryAdmin> dormitoryAdminPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<DormitoryAdmin> dormitoryAdminPage1 = null;
        if (searchForm.getValue().equals("")){
           dormitoryAdminPage1 = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, null);
        }else {
            QueryWrapper<DormitoryAdmin> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),searchForm.getValue());
            dormitoryAdminPage1 = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, queryWrapper);
        }
      log.info("宿管人数:{}",dormitoryAdminPage1.getTotal());
      return new PageVO(dormitoryAdminPage1.getRecords(),dormitoryAdminPage1.getTotal());
    }
}
